//
//  BaseNavigationController.swift
//  sinaOnceAgain
//
//  Created by 王相超 on 16/2/24.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit

class BaseNavigationController: UINavigationController,UIGestureRecognizerDelegate {
    
    
    //控制器入口
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //一旦自定义了返回按钮 原有的返回手势 就会自动失效
        self.interactivePopGestureRecognizer?.delegate = self
    }
    
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        let count = childViewControllers.count
        
        if count != 0 {
            
            viewController.hidesBottomBarWhenPushed = true
            
            let btn = UIButton(backImageName: nil, title: "返回", textColor: UIColor.darkGrayColor(), imageName: "navigationbar_back_withtext", fontSize: 14)
            btn.addTarget(self, action: "back", forControlEvents: .TouchUpInside)
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
            
        }
        
        
        super.pushViewController(viewController, animated: animated)
   
    }
    
    @objc private func back() {
        self.popViewControllerAnimated(true)
    }
    
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool{
        
        return childViewControllers.count > 1
    }

}
